package com.bobo.demo.Thread.sync;


//不安全的买票
public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket station = new BuyTicket();
        new Thread(station,"黄牛").start();
        new Thread(station,"学生党").start();
        new Thread(station,"黑客").start();
    }

}

//买票方法

class BuyTicket implements Runnable{

    private Integer ticketNumber = 50;//票有多少张

    private boolean flag = true;

    @Override
    public void run() {
        while(flag) {
            buy();
        }

    }
    synchronized void buy(){
        if(ticketNumber <= 0) {
            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"抢到了第"+ticketNumber--+"张票");
    }
}
